package tests.第三方工具类测试使用.mapstruct;

import lombok.Data;
import lombok.ToString;
import org.junit.Test;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Named;
import org.mapstruct.factory.Mappers;
import tests.第三方工具类测试使用.mapstruct.pojo.HeathEnum;
import tests.第三方工具类测试使用.mapstruct.pojo.SexEnum;

/**
 * @author: SunAo
 * @create: 2020-09-03 14:35
 */
public class UseOtherClass {

    @Data
    static class Source{
        private String name;
        private Integer sexCode;
        private Integer heathCode;
    }
    @Data
    @ToString
    static class Target{
        private String nickname;
        private String sexName;
        private String heathName;
    }
    @Mapper(uses = Trans.class)
    interface Covert{
        QualifiedTest.Covert INSTACE = Mappers.getMapper(QualifiedTest.Covert.class);

        @Mapping(source = "name",target = "nickname")
        @Mapping(source = "sexCode",target = "sexName",qualifiedByName = "sexTrans")
        @Mapping(source = "heathCode",target = "heathName",qualifiedByName = "heathTrans")
        QualifiedTest.Target to(QualifiedTest.Source source);
    }
    static class Trans{
        @Named("sexTrans")
        private String sexCode2Name(Integer code){
            for (SexEnum value : SexEnum.values()) {
                if(value.getCode().equals(code)) return value.getName();
            }
            return null;
        }
        @Named("heathTrans")
        private String heathCode2Name(Integer code){
            for (HeathEnum value : HeathEnum.values()) {
                if(value.getCode().equals(code)) return value.getName();
            }
            return null;
        }
    }

    static QualifiedTest.Source source;
    static {
        source = new QualifiedTest.Source();
        source.setName("SunAo");
        source.setSexCode(SexEnum.MAN.getCode());
        source.setHeathCode(HeathEnum.Heath.getCode());
    }

    @Test
    public void to(){
        QualifiedTest.Target target = QualifiedTest.Covert.INSTACE.to(source);
        System.out.println(target.toString());
    }


}
